import { z } from 'zod'

import { baseManyObjectsScheme, baseScheme } from '@/types/base.types'

export type forObject = { name: string; id: number }

export const attachGroupScheme = baseScheme.extend({
	name: z.string().min(2).max(35),
	description: z
		.string()
		.nullable()
		.transform(value => (value === null ? '' : value))
})

export const attachGroupFormScheme = attachGroupScheme.omit({
	created_on: true,
	updated_on: true
})

export type IAttachGroup = z.infer<typeof attachGroupScheme>

export const attachGroupsResponseScheme = baseManyObjectsScheme.extend({
	results: z.array(attachGroupScheme)
})

export type IAttachGroups = z.infer<typeof attachGroupsResponseScheme>

export const attachScheme = baseScheme.extend({
	url: z.string(),
	original_name: z
		.string()
		.min(4)
		.regex(
			/^[-\wа-яА-Я^&'@{}[\],$=!#().%+~ ]+\.\w+$/,
			'Некорректное имя файла'
		),
	content_type: z.string(),
	size: z.number(),
	group: attachGroupScheme.nullable(),
	creator: z.number(),
	request: z.number().nullish(),
	contract: z.number().nullish(),
	warranty: z.number().nullish(),
	reclamation: z.number().nullish(),
	delivery: z.number().nullish(),
	assembly: z.number().nullish(),
	worker: z.number().nullish()
})

export type IAttach = z.infer<typeof attachScheme>
export type groupedAttachs = {
	[key: number]: IAttach[]
}

export const attachUpdateScheme = attachScheme
	.omit({
		size: true,
		content_type: true,
		creator: true,
		created_on: true,
		updated_on: true,
		group: true
	})
	.extend({
		group: z.number().nullish()
	})

export type IAttachUpdate = z.infer<typeof attachUpdateScheme>

export const attachPreviewScheme = attachScheme
	.omit({
		creator: true,
		created_on: true,
		updated_on: true,
		request: true,
		contract: true,
		warranty: true,
		reclamation: true,
		delivery: true,
		assembly: true,
		worker: true
	})
	.extend({
		url: z.string().optional(),
		file: z.any()
	})

export type IAttachPreview = z.infer<typeof attachPreviewScheme>

export const attachsScheme = baseManyObjectsScheme.extend({
	results: z.array(attachScheme)
})

export type IAttachs = z.infer<typeof attachsScheme>
